﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.DBContext;
using PmSoft.Repositories;
using PmSoft.Utilities;

namespace Modules.Manager.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ManagerInfoRepository : Repository<ManagerInfo, DefaultDbContext>
    {
        private readonly ILogger logger;

        public ManagerInfoRepository(DefaultDbContext dbContext, IApplicationContext applicationContext, ILogger<ManagerInfoRepository> logger)
            : base(dbContext, applicationContext)
        {
            this.logger = logger;
        }

        public bool CardNoExists(string cardno)
        {
            return DbContext.Exists<ManagerInfo>("cardno=@0", StringUtility.StripSQLInjection(cardno));
        }

        /// <summary>
        /// 更新默认管理的项目ID
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public bool UpdateDefaultProjectId(ManagerInfo manager, int projectId)
        {
            if (manager == null)
                return false;
            var sql_update = Sql.Builder;
            sql_update.Append("Update manager SET project_id = @0 where manager_id = @1", projectId, manager.ManagerId);//局部更新数据库
            int affectCount = DbContext.Execute(sql_update);
            if (affectCount > 0)
            {
                manager.ProjectId = projectId;
                base.OnUpdated(manager);//更新内存数据
                return true;
            }
            return false;
        }

        public ManagerInfo? GetManagerInfo(string cardNo)
        {
            Sql sql = Sql.Builder.Select("manager_id").From("manager").Where("cardno=@0", cardNo);
            var id = DbContext.FirstOrDefault<int>(sql);
            if (id == 0)
                return null;
            return Get(id);
        }

        public IPagedList<ManagerInfo> GetManagers(ManagerQuery query, int pageIndex, int pageSize)
        {
            Sql sql = Sql.Builder;
            sql.Select("manager_id")
                .From("manager");
            if (!string.IsNullOrEmpty(query.Keywords))
                sql.Where("(name Like @0 or mobile Like @0 or cardno Like @0)", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
            sql.OrderBy("create_time DESC");
            return GetPagingEntities(pageSize, pageIndex, sql);
        }

        /// <summary>
        /// author:hzj date:2023/11/14 describe:新增编辑操作
        /// </summary>
        /// <param name="managerInfo"></param>
        /// <returns></returns>
        public ManagerInfo AddOrUpdateManagerInfo(ManagerInfo managerInfo)
        {
            if (managerInfo.ManagerId > 0)
            {
                Update(managerInfo);
            }
            else
            {
                Insert(managerInfo);
            }
            return managerInfo;
        }
    }
}